

<%@page import="gameServlet.NewPlayerServlet"%>
<%@page import="submarinesControl.SubmarinesControl"%>
<%@page import="gameServlet.GameServlet"%>
<%@page import="submarinesControl.Player"%>
<%@page import="submarinesControl.GameManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>

<%
SubmarinesControl scCurrSubmarines = (SubmarinesControl)getServletContext().getAttribute(GameServlet.gameSessionName);
int nPlayerNum = (Integer)request.getSession().getAttribute(NewPlayerServlet.playerNum);
//String playerID = request.getSession().getId();
GameManager myManager = scCurrSubmarines.getGameManager();
Player myPlayers[] = scCurrSubmarines.players;
//int nPlayerNum = 0;

int currTurnIndex = myManager.getCurrentTurn();
int tableSize = 10;
int numOfPlayers = myManager.getNumOfPlayers();
%>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css"/>
        <script type="text/javascript" src="jquery.js"></script>
        <script>         
            function displayOtherGrid(newDisplay) {
                $("#gridToAttack_"+currentOtherGridName).addClass("hideGrid");
                $("#gridToAttack_"+newDisplay).removeClass("hideGrid");
                currentOtherGridName = newDisplay;
            }
            
            function bombPlayer(cellId) {
                //currentOtherGridName
                currentPlayer = "<%= myPlayers[currTurnIndex].getName() %>";
                //$.get("game", {player : currentPlayer, attack : currentOtherGridName, cell : cellId, action : 'bomb'}, null);
                cellId = cellId.toLowerCase();
                document.location.href='game?player='+ currentPlayer +'&attack='+ currentOtherGridName +'&position='+ cellId +'&action=bomb';
            }
        </script>
    </head>
    <body>

<%
// check for WIN
if (numOfPlayers == 1) {
%>
    <center>
        <iframe width="420" height="315" src="http://www.youtube.com/embed/Buy-cTGSaEE" frameborder="0" allowfullscreen></iframe>
        <h1><%= myPlayers[currTurnIndex].getName() %> won the Yellow Submarine!</h1>
        <button id="newGameButton" type="button" onclick="">New Game</button>
    </center>
<%
}
else {
%>
  <div align="center">
<!-- Current active player -->
<div style="float:left" align="center">
         
    <h3>Your name is <span style="color:#00ff00"><%= myPlayers[nPlayerNum].getName() %></span><span>
            
<%
    
 if (myPlayers[nPlayerNum].isHuman() == false) {
        
%>
     <button id="cpuButton" type="button" onclick="bombPlayer('A1')">Play CPU</button>
<%
    }
%>  </span></h3>
    <table>
<%
for (int numIndex=-1; numIndex<tableSize; numIndex++) {
%>
        <tr>
<%
    for (int letIndex=-1; letIndex<tableSize; letIndex++) {
        
        // Print corner cell
        if ((numIndex == -1) && (letIndex == -1)) {
%>
            <td class="">
                &nbsp;
            </td>
<%
        }
        
        // Print the letters 
        if ((numIndex == -1) && (letIndex != -1)) {
%>
            <td class="gridSides">
                <center> <%= ((char)(letIndex+65)) %> </center>
            </td>
<%
        }
        
        // Print the numbers
        if ((numIndex != -1) && (letIndex == -1)) {
%>
            <td class="gridSides">
                <center> <%= numIndex+1 %> </center>
            </td>
<%
        }
        
        // Print the cells
        if ((letIndex != -1) && (numIndex != -1)) {
                      
            String cellSubStatus = "";
            boolean isSubmarine = myPlayers[nPlayerNum].getBoard().getBoard()[numIndex][letIndex].isSubmarine();
            boolean isBombed = myPlayers[nPlayerNum].getBoard().getBoard()[numIndex][letIndex].isBombed();
            
            if ((isSubmarine == true) && (isBombed == true)) {
                cellSubStatus = "cellBombed";
            }
            else if (isSubmarine == true) {
                cellSubStatus = "cellFilled";
            }
            else if (isBombed == true) {
                cellSubStatus = "cellGrayed";
            }
            else {
                cellSubStatus = "cellEmpty";
            }
                
%>
            <td>
    
                <div id="<%= ((char)(letIndex+65))+""+(numIndex+1)%>" class="theHand cellBackground <%= cellSubStatus %>">
                    &nbsp;
                </div>
            </td>
<%
       }
    }
%>
        </tr>
<%
}
%>
    </table> 
</div>
<!--    
        <div class="middleDiv">
            &nbsp;
        </div>
-->

<%
// other players grids
boolean isFirstPlayer = true;
for (int i=0; i<numOfPlayers; i++) {

    // don't display the current active player
    if (nPlayerNum != i) {
        
        if (isFirstPlayer == true) {
            isFirstPlayer = false;
%>
        <script>
            var currentOtherGridName = "<%= myPlayers[i].getName() %>";
        </script>

        <div id="gridToAttack_<%= myPlayers[i].getName() %>" class="" style="float:right" >
<%
        } else { 
%>
        <div id="gridToAttack_<%= myPlayers[i].getName() %>" class="hideGrid" style="float:right" >
<%
        }
%>
            <h3>Player to attack: <span style="color:#ff0000"><%= myPlayers[i].getName() %></span></h3>
            <table>
<%
for (int numIndex=-1; numIndex<tableSize; numIndex++) {
%>
                <tr>
<%
    for (int letIndex=-1; letIndex<tableSize; letIndex++) {
        
        // Print corner cell
        if ((numIndex == -1) && (letIndex == -1)) {
%>
                    <td class="">
                        &nbsp;
                    </td>
<%
        }
        
        // Print the letters 
        if ((numIndex == -1) && (letIndex != -1)) {
%>
                    <td class="gridSides">
                        <center> <%= ((char)(letIndex+65)) %> </center>
                    </td>
<%
        }
        
        // Print the numbers
        if ((numIndex != -1) && (letIndex == -1)) {
%>
                    <td class="gridSides">
                        <center> <%= numIndex+1 %> </center>
                    </td>
<%
        }
        
        // Print the cells
        if ((letIndex != -1) && (numIndex != -1)) {
            
            String cellSubStatus = "";
            boolean isSubmarine = myPlayers[i].getBoard().getBoard()[numIndex][letIndex].isSubmarine();
            boolean isBombed = myPlayers[i].getBoard().getBoard()[numIndex][letIndex].isBombed();
            
            if ((isSubmarine == true) && (isBombed == true)) {
                cellSubStatus = "cellBombed";
            }
            else if (isBombed == true) {
                cellSubStatus = "cellGrayed";
            }
            else {
                cellSubStatus = "cellEmpty";
            }
            
            String cellId = ((char)(letIndex+65))+""+(numIndex+1);
            
            
%>
                  <td>
<%
            if (myPlayers[nPlayerNum].isHuman() == true) {
%>
                        <div id="<%= cellId%>" class="theHand cellBackground <%= cellSubStatus %>" onclick="bombPlayer('<%= cellId %>')">
<%
            }
            else {
%>
                        <div id="<%= cellId%>" class="cellBackground <%= cellSubStatus %>">
<%
            }
%>
                            &nbsp;
                        </div>
                    </td>
<%
       }
    }
%>
                </tr>
<%
}
%>
            </table>
        </div>
<%
   }

}
%>        

    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><hr><br/>


<!-- Grids of other players -->

    <div style="float:right; "><h4>Select player you would like to attack&nbsp;&nbsp;&nbsp;&nbsp;</h4>
            <h3>
<%
for (int i=0; i<numOfPlayers; i++) {
    if (nPlayerNum != i) {
%>
    <b><a class="theHand" id="otherPlayer_<%= myPlayers[i].getName() %>" onclick="displayOtherGrid('<%= myPlayers[i].getName() %>')"><%= myPlayers[i].getName() %></a></b>&nbsp;&nbsp;
<%
    }
}
%>
    &nbsp;&nbsp;&nbsp;&nbsp;</h3></div>
    
    <div style="float:left">
<%
    String mylist[] = myManager.moveData();
    if (mylist[2] != null) {
%>
        Last game play was by <b><%= mylist[0] %>.</b><br/>
        He attacked <b><%= mylist[1] %></b> at cell <b><%= mylist[2] %></b><br/>
        and managed to <u><%= mylist[3] %></u>
<%
   }
%>
    </div>
<br/>
<h3> It's <b><%= myPlayers[currTurnIndex].getName() %></b> turn </h3>
<%
}
%>
    </body>
</html>
